const request = function (obj) {
  let key = "cookie";
  let cookie = wx.getStorageSync(key);

  if (!("fail" in obj)) {
    obj.fail = function (err) {};
  }
  if (!("complete" in obj)) {
    obj.complete = function (res) {};
  }

  wx.request({
    url: obj.url,
    data: obj.data,
    method: obj.method,
    header: {
      "Content-Type": "application/x-www-form-urlencoded",
      Cookie: cookie,
    },
    success: (res) => {
      if (res.header) {
        if ("Set-Cookie" in res.header) {
          wx.setStorageSync(key, res.header["Set-Cookie"]);
        } else if ("set-cookie" in res.header) {
          wx.setStorageSync(key, res.header["set-cookie"]);
        }
      }
      obj.success(res);
    },
    fail: (err) => {
      if (res.header) {
        if ("Set-Cookie" in res.header) {
          wx.setStorageSync(key, res.header["Set-Cookie"]);
        } else if ("set-cookie" in res.header) {
          wx.setStorageSync(key, res.header["set-cookie"]);
        }
      }
      obj.fail(err);
    },
    complete: (res) => {
      obj.complete(res);
    },
  });
};

module.exports = {
  request: request,
};

const request1 = function (obj) {
  //设置cookie缓存
  if (obj.fail) {
    obj.fail = function (err) {
      wx.setStorageSync("cookie", err.header["Set-Cookie"]);
      obj.fail(err);
    };
  } else {
    obj.fail = function (err) {
      wx.setStorageSync("cookie", err.header["Set-Cookie"]);
    };
  }
  if (obj.success) {
    obj.success = function (res) {
      wx.setStorageSync("cookie", res.header["Set-Cookie"]);
      obj.success(res);
    };
  } else {
    obj.success = function (res) {
      wx.setStorageSync("cookie", res.header["Set-Cookie"]);
    };
  }

  //设置请求头
  if (obj.header) {
    obj.header = {
      Cookie: wx.getStorageSync("cookie"),
      "Content-Type": "application/x-www-form-urlencoded",
      ...obj.header,
    };
  } else {
    obj.header = {
      Cookie: wx.getStorageSync("cookie"),
      "Content-Type": "application/x-www-form-urlencoded",
    };
  }

  wx.request(obj);
};

module.exports = {
  request1: request1,
};
const Promise = require("./es6-promise.js");

//封装Request请求方法
function requst(url, method, data = {}) {
  const app = getApp();
  if (app.globalData.token) {
    //console.log("token有值:" + app.globalData.token);
    data.token = app.globalData.token;
  }

  wx.showNavigationBarLoading();
  data.method = method;
  return new Promise((resove, reject) => {
    wx.request({
      url: url,
      data: data,
      header: {
        Cookie: wx.getStorageSync("cookie"),
        "Content-Type": "application/x-www-form-urlencoded",
      },
      method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function (res) {
        wx.hideNavigationBarLoading();
        wx.setStorageSync("cookie", res.header["Set-Cookie"]);
        resove(res.data);
      },
      fail: function (msg) {
        console.log("reqest error", msg);
        wx.hideNavigationBarLoading();
        wx.setStorageSync("cookie", res.header["Set-Cookie"]);
        reject("fail");
      },
    });
  });
}

module.exports = {
  requst: requst,
};

var util = require("../../../utils/util"); //工具类

/**
 * 获得收藏商家列表
 */
const getFavoriteShop = (e) => {
  wx.showToast({ title: "加载中...", icon: "loading" }); //显示加载框
  let that = this;
  let url = host + "api/prints/user/favoriteshoplist";
  let method = method;
  let data = {
    user_id: user_id,
    page: this.data.page,
    size: this.data.size,
  };
  let result = util.requst(url, method, data);
  result
    .then(function (res) {
      wx.hideToast(); //隐藏加载框
      console.log(res);
      if (res.code == 0) {
        let shop_list = res.data;
        //数组合并
        that.data.shop_list = that.data.shop_list.concat(shop_list);
        that.data.page++;
        that.setData({ shop_list: that.data.shop_list, page: that.data.page });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none", // 支持 icon 组件的所有 type 类型及 none 值
          duration: 1000,
          mask: true,
        });
        return false;
      }
    })
    .catch(function (res) {
      wx.hideToast(); //隐藏加载框
      //console.log(res);
    });
};
